//
//  NSMutableAttributedString+Com_StringAttribute.m
//  WEIXUE_RN
//
//  Created by WangGuoli on 2017/11/17.
//  Copyright © 2017年 zhanghao. All rights reserved.
//

#import "NSMutableAttributedString+Com_StringAttribute.h"
#import "NSString+Com_Range.m"

@implementation NSMutableAttributedString (Com_StringAttribute)
/**
 *  添加富文本对象
 *
 *  @param stringAttribute 实现了StringAttributeProtocol协议的对象
 */
- (void)addStringAttribute:(Com_StringAttribute *)stringAttribute {
  
  if (stringAttribute.effectiveRange.location != NSNotFound &&[stringAttribute attributeValue] ) {
    
    [self addAttribute:[stringAttribute attributeName]
                 value:[stringAttribute attributeValue]
                 range:[stringAttribute effectiveRange]];
  }
}

/**
 *  消除指定的富文本对象
 *
 *  @param stringAttribute 实现了StringAttributeProtocol协议的对象
 */
- (void)removeStringAttribute:(Com_StringAttribute *)stringAttribute {
  
  if (stringAttribute.effectiveRange.location != NSNotFound) {
    
    [self removeAttribute:[stringAttribute attributeName]
                    range:[stringAttribute effectiveRange]];
  }
}



/**
 添加 字体属性
 
 @param font 字体
 @param partStr 部分字符串
 @param allStr 所有的字符串
 @param isRecursive 是否递归去添加属性
 */
- (void)addFontAttributeWithFont:(UIFont *)font
                         partStr:(NSString *)partStr
                          allStr:(NSString *)allStr
                     isRecursive:(BOOL)isRecursive {
  if (partStr.length<=0) {
    partStr =@"";
  }
  if (isRecursive) {
    
    //递归去查询 整个字符串
    NSArray * rangeArr = [allStr rangeValuesOfSearchString:partStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, allStr.length)];
    for (NSValue * rangeValue in rangeArr) {
      
      NSRange range = [rangeValue rangeValue];
      Com_FontStringAttribute * partFontAttribute = [[Com_FontStringAttribute alloc]init];
      partFontAttribute.effectiveRange = range;
      partFontAttribute.attributeValue = font;
      [self addStringAttribute:partFontAttribute];
    }
  } else {
    
    Com_FontStringAttribute * partFontAttribute = [[Com_FontStringAttribute alloc]init];
    partFontAttribute.effectiveRange = [allStr rangeOfString:partStr];
    partFontAttribute.attributeValue = font;
    [self addStringAttribute:partFontAttribute];
  }
  
}

/**
 添加 字体颜色属性
 
 @param color  颜色
 @param partStr 部分字符串
 @param allStr 所有的字符串
 @param isRecursive 是否递归去添加属性
 */
- (void)addForegroundColorAttributeWithColor:(UIColor *)color
                                     partStr:(NSString *)partStr
                                      allStr:(NSString *)allStr
                                 isRecursive:(BOOL)isRecursive {
  if (partStr.length<=0) {
    partStr =@"";
  }
  if (isRecursive) {
    
    //递归去查询 整个字符串
    NSArray * rangeArr = [allStr rangeValuesOfSearchString:partStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, allStr.length)];
    for (NSValue * rangeValue in rangeArr) {
      
      NSRange range = [rangeValue rangeValue];
      Com_ForegroundColorStringAttribute * partForeAttribute = [[Com_ForegroundColorStringAttribute alloc]init];
      partForeAttribute.effectiveRange = range;
      partForeAttribute.attributeValue = color;
      [self addStringAttribute:partForeAttribute];
    }
  } else {
    
    Com_ForegroundColorStringAttribute * partForeAttribute = [[Com_ForegroundColorStringAttribute alloc]init];
    partForeAttribute.effectiveRange = [allStr rangeOfString:partStr];
    partForeAttribute.attributeValue = color;
    [self addStringAttribute:partForeAttribute];
  }
  
}

- (void)addLineSpaceAttributeWithSpace:(CGFloat )lineSpace textAlignment:(NSTextAlignment )textAlignment
                               partStr:(NSString *)partStr
                                allStr:(NSString *)allStr
                           isRecursive:(BOOL)isRecursive {
  if (partStr.length<=0) {
    partStr =@"";
  }
  if (isRecursive) {
    
    //递归去查询 整个字符串
//    NSArray * rangeArr = [allStr rangeValuesOfSearchString:partStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, allStr.length)];
//    for (NSValue * rangeValue in rangeArr) {
    
//      NSRange range = [rangeValue rangeValue];
//      Com_ForegroundColorStringAttribute * partForeAttribute = [[Com_ForegroundColorStringAttribute alloc]init];
//      partForeAttribute.effectiveRange = range;
//      partForeAttribute.attributeValue = color;
//      [self addStringAttribute:partForeAttribute];
//    }
  } else {
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.alignment = textAlignment;
    
    Com_LineSpaceStringAttribute * partForeAttribute = [[Com_LineSpaceStringAttribute alloc]init];
    partForeAttribute.effectiveRange = [allStr rangeOfString:partStr];
    partForeAttribute.attributeValue = paragraphStyle;
    [self addStringAttribute:partForeAttribute];
    
  }
  
}

- (void)appImgWithOrigin:(UIImage *)img Height:(CGFloat)height{
  if (!img) {
    return;
  }
  //设置Attachment
  NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  //使用一张图片作为Attachment数据
  
  attachment.image = img;
  //这里bounds的x值并不会产生影响
  attachment.bounds=CGRectMake(0,  -(img.size.height - height)/2, img.size.width, img.size.height);
  
  
  [self appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];

}
@end
